﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using zhangmenren.IService;
using zhangmenren.Models;
using zhangmenren.Untils;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace zhangmenren.Areas.Admin.Controllers
{
    [Area(areaName: "Admin")]
    public class LoginController : Controller
    {

        private IAdminService _adminService;
        public LoginController(IAdminService adminService)
        {
            _adminService = adminService;
        }

        // GET: /<controller>/
        public IActionResult Index()
        {
            if (Cookie.GetCookie<AdminEntity>(HttpContext)!=null)
            {
                return RedirectToAction("Index", "Admin");
            }
            return View();
        }

        [HttpPost]
        public async Task<JsonResult> Login(AdminEntity admin)
        {
            if (!string.IsNullOrWhiteSpace(admin.UserName) && !string.IsNullOrWhiteSpace(admin.Pwd))
            {
                var result = await _adminService.GetAdmin(admin);
                if (result.Count() > 0)
                {
                    Cookie.SaveCookie<AdminEntity>(Request.HttpContext, admin);
                }
                else
                {
                    return Json(new { State = "FAIL", Msg = "用户名或密码错误！" });
                }
            }
            else
            {
                return Json(new { State = "FAIL", Msg = "用户名和密码不能为空！" });
            }

            return Json(new { State = "OK" });
        }







    }
}
